package com.ruoyi.system.controller;

import cn.hutool.core.util.RandomUtil;
import com.ruoyi.common.core.constant.CacheConstants;
import com.ruoyi.common.core.constant.Constants;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.oldUtils.SpringUtils;
import com.ruoyi.common.redis.utils.RedisUtils;
import com.ruoyi.message.config.SmsProperties;
import com.ruoyi.message.core.IAliyunSmsTemplate;
import com.ruoyi.message.entity.SmsResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;
import java.time.Duration;
import java.util.HashMap;
import java.util.Map;

/**
 * 验证码操作处理
 *
 * @author Lion Li
 * @version 4.0.0
 */
@RestController
@RequestMapping("/sms")
public class CaptchaController {

    @Resource
    private SmsProperties smsProperties;

    /**
     * 发送短信验证码
     *
     * @param phoneNumber 用户手机号
     *
     * @version 4.0.0
     */
    @GetMapping("/send")
    public R<Void> smsCaptcha(@NotBlank(message = "手机号不能为空")
                              String phoneNumber) {
        if (!smsProperties.getEnabled()) {
            return R.fail("当前系统没有开启短信功能！");
        }

        // 判断时间间隔
        String oldKey = CacheConstants.CAPTCHA_CODE_KEY + phoneNumber;
        if (RedisUtils.isExistsObject(oldKey)){
            return R.fail("时间间隔不能小于2分钟，请稍后再试");
        }

        String key = CacheConstants.CAPTCHA_CODE_KEY + phoneNumber;
        String code = RandomUtil.randomNumbers(6);
        RedisUtils.setCacheObject(key, code, Duration.ofMinutes(Constants.CAPTCHA_EXPIRATION));
        // 验证码模板id
        String templateId = smsProperties.getValidateTemplate();
        Map<String, String> map = new HashMap<>(1);
        map.put("code", code);
        IAliyunSmsTemplate smsTemplate = SpringUtils.getBean(IAliyunSmsTemplate.class);
        SmsResult result = smsTemplate.send(phoneNumber, templateId, map);
        if (!result.isSuccess()) {
            System.out.println("验证码短信发送异常 => " + result);
            return R.fail(result.getMessage());
        }
        return R.ok();
    }

    /**
     * 验证短信验证码
     *
     * @param phoneNumber 用户手机号
     * @param captcha 验证码
     * @version 4.0.0
     */
    @GetMapping("/validate")
    public R<Void> validateCaptcha(@NotBlank(message = "手机号不能为空") String phoneNumber,
                                   @NotBlank(message = "验证码不能为空") String captcha) {
        if (!smsProperties.getEnabled()) {
            return R.fail("当前系统没有开启短信功能！");
        }
        String key = CacheConstants.CAPTCHA_CODE_KEY + phoneNumber;
        if (!RedisUtils.isExistsObject(key)){
            return R.fail("验证码已超时，请重新获取");
        }
        String code = RedisUtils.getCacheObject(key).toString();

        if (!captcha.equals(code)){
            System.out.println("验证码短信发送异常 => " + code);
            return R.fail("验证码错误，请重新输入");
        }
        return R.ok();
    }

}
